<?php
session_start();
include("load-settings.php");

require_once("unirest/lib/Unirest.php");

$order = mysql_real_escape_string($_POST['order']);

$result = mysql_query("SELECT * FROM order_record WHERE id = $order") or die("SELECT * FROM order_record WHERE id = $order");

if(mysql_num_rows($result) == 0)
	die("Invalid order ID");

$row = mysql_fetch_array($result);
$user = $row['user'];
$summoner = $row['summoner'];
$region = $row['region'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);


/*$response = Unirest::get(
  "https://teemojson.p.mashape.com/player/$region/$summoner/recent_games",
  array(
    "X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  )
);*/

$page = $_POST['page'];

//$x = ($page-1)*5;
$x = ($page-1)*5;
$y = $x;

$returnArray = array();

/*$decode = json_decode($response->raw_body)->data->gameStatistics->array;

while(isset($decode[$x]))
{
	$match_array = array();

	$match_array['champion'] = $decode[$x]->championId;
	$match_array['date'] = $decode[$x]->createDate;
	$match_array['timestamp'] = strtotime($decode[$x]->createDate);
	$match_array['elo'] = $decode[$x]->eloChange;
	$match_array['spell1'] = $decode[$x]->spell1;
	$match_array['spell2'] = $decode[$x]->spell2;

	foreach($decode[$x]->statistics->array as $stat)
	{
		if($stat->statType == "CHAMPIONS_KILLED")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['kills'] = 0;
			else
				$match_array['kills'] = $stat->value;
		}

		if($stat->statType == "ASSISTS")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['assists'] = 0;
			else
				$match_array['assists'] = $stat->value;
		}

		if($stat->statType == "WIN")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['win'] = 0;
			else
				$match_array['win'] = $stat->value;
		}

		if($stat->statType == "GOLD_EARNED")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['gold'] = 0;
			else
				$match_array['gold'] = $stat->value;
		}

		if($stat->statType == "MINIONS_KILLED")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['minions'] = 0;
			else
				$match_array['minions'] = $stat->value;
		}

		if($stat->statType == "NUM_DEATHS")
		{
			if(!isset($stat->value) || $stat->value == "")
				$match_array['deaths'] = 0;
			else
				$match_array['deaths'] = $stat->value;
		}

		if($stat->statType == "ITEM0")
		{
			$match_array['item0'] = $stat->value;
		}
		if($stat->statType == "ITEM1")
		{
			$match_array['item1'] = $stat->value;
		}
		if($stat->statType == "ITEM2")
		{
			$match_array['item2'] = $stat->value;
		}
		if($stat->statType == "ITEM3")
		{
			$match_array['item3'] = $stat->value;
		}
		if($stat->statType == "ITEM4")
		{
			$match_array['item4'] = $stat->value;
		}
		if($stat->statType == "ITEM5")
		{
			$match_array['item5'] = $stat->value;
		}

		if(!isset($match_array['deaths']))
		{
			$match_array['deaths'] = 0;
		}
		if(!isset($match_array['kills']))
		{
			$match_array['kills'] = 0;
		}
		if(!isset($match_array['assists']))
		{
			$match_array['assists'] = 0;
		}
		if(!isset($match_array['minions']))
		{
			$match_array['minions'] = 0;
		}
		if(!isset($match_array['gold']))
		{
			$match_array['gold'] = 0;
		}
	}
	if($match_array['timestamp'] >= $order_row['timestamp'])
	{
		array_push($returnArray, $match_array);
		$y++;
	}
	$x++;
}
$pages = ceil($x/5);

usort($returnArray, function($a, $b) {
    return $b['timestamp'] - $a['timestamp'];
});

//print_r($returnArray); die;
*/
/*$response = Unirest::get(
  "https://teemojson.p.mashape.com/player/".$region."/".$summoner."/leagues",
  array(
    "X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  )
);

$decode = json_decode($response->raw_body);

$x = 0;

$tier = "UNRANKED";
$rank = "";

while(isset($decode->data->summonerLeagues->array[$x]))
{
	if($decode->data->summonerLeagues->array[$x]->queue == "RANKED_SOLO_5x5")
	{
		$tier = $decode->data->summonerLeagues->array[$x]->tier;
		$rank = $decode->data->summonerLeagues->array[$x]->requestorsRank;
	}
	$x++;
}*/

$start = ($page-1) * 5;

$result = mysql_query("SELECT * FROM match_history WHERE order_id = $order ORDER BY timestamp DESC LIMIT $start, 5") or die(mysql_error());

while($row = mysql_fetch_array($result))
{
	array_push($returnArray, $row);
}

$x = 0;
$champ_array = array();

while(isset($returnArray[$x]))
{
	$champ_array[$x] = $returnArray[$x]['champion'];
	$x++;
}

$champs = array_count_values($champ_array);

arsort($champs);
$champ_keys = array_keys($champs);

$x = 0;

$win_loss = array();

while(isset($champ_keys[$x]) && $x < 3)
{
	$y = 0;
	$win_loss[$x] = array();
	$win_loss[$x]['win'] = 0;
	$win_loss[$x]['loss'] = 0;
	while(isset($returnArray[$y]))
	{
		if($returnArray[$y]['champion'] == $champ_keys[$x])
		{
			if($returnArray[$y]['win'] == 1)
				$win_loss[$x]['win']++;
			else
				$win_loss[$x]['loss']++;
		}
		$y++;
	}
	$x++;
}

$champ_lookup = array();

$response = Unirest::get(
  "https://teemojson.p.mashape.com/datadragon/champion",
  array(
    "X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  )
);

$decode = json_decode($response->raw_body)->data;
$x = 0;

foreach ($decode as $item)
{
	$champ_lookup[$x] = array();
	$champ_lookup[$x]['id'] = $item->key;
	$champ_lookup[$x]['name'] = $item->id;
	$x++;
}
//var_dump($champ_lookup); die;

//var_dump($champ_lookup[key_to_name($champ_lookup, (string)$returnArray[0]['champion'])]['name']); die;

function key_to_name($products, $needle)
{
   foreach($products as $key => $product)
   {
      if ( $product['id'] === $needle )
         return $key;
   }
   return false;
}
//var_dump($decode->data->summonerLeagues->array[0]); die;
function numberToRoman($num) 
 {
     // Make sure that we only use the integer portion of the value
     $n = intval($num);
     $result = '';
 
     // Declare a lookup array that we will use to traverse the number:
     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
 
     foreach ($lookup as $roman => $value) 
     {
         // Determine the number of matches
         $matches = intval($n / $value);
 
         // Store that many characters
         $result .= str_repeat($roman, $matches);
 
         // Substract that from the number
         $n = $n % $value;
     }
 
     // The Roman numeral should be built, return it
     return $result;
 }
//$x = ($page-1) * 5;
$x = 0;

while(isset($returnArray[$x]) && $x < 5)
{

	/*if($returnArray[$x]['win'] == 0)
		echo '<tbody class="match_loss" cellpadding="0" cellspacing="0">';
	else
		echo '<tbody class="match_win" cellpadding="0" cellspacing="0">';

	echo '<tr><td><div style="background-image: url(/dashboard/images/champions/'.$returnArray[$x]['champion'].'.png); background-size: contain; height: 72px; width: 72px;"></div></td>';

	if($returnArray[$x]['win'] == 0)
		echo '<td class="center">Loss</td>';
	else
		echo '<td class="center">Win</td>';

	echo '<td class="center"><small>'.$returnArray[$x]['date'].'</small> <br><small><i>'.$tier.' '.$rank.', LP: '.$returnArray[$x]['elo'].'</i></small></td>';

	echo '<td class="center">'.$returnArray[$x]['kills'].' Kills<br>'.$returnArray[$x]['deaths'].' Deaths<br>'.$returnArray[$x]['assists'].' Assists</td>';

	echo '<td class="center">'.$returnArray[$x]['gold'].'</td>';

	echo '<td class="center">'.$returnArray[$x]['minions'].'</td>';

	echo '<td class="center summoner-spells"><img src="/dashboard/images/summoner/'.$returnArray[$x]['spell1'].'.png"><img src="/dashboard/images/summoner/'.$returnArray[$x]['spell2'].'.png"></td>';

	echo '<td class="center">';

	if(isset($returnArray[$x]['item0']) && $returnArray[$x]['item0'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item0'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item1']) && $returnArray[$x]['item1'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item1'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item2']) && $returnArray[$x]['item2'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item2'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item3']) && $returnArray[$x]['item3'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item3'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item4']) && $returnArray[$x]['item4'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item4'].'_32.png" alt="">';
	}
	if(isset($returnArray[$x]['item5']) && $returnArray[$x]['item5'] != 0)
	{
		echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item5'].'_32.png" alt="">';
	}
	   echo '<br>';
	   //echo '<small>BOOSTER: nono29</small>';
					 echo '</td>';
	echo '</tr></tbody>';

	$x++;*/
				if($returnArray[$x]['win'] == 0)
					echo '<div class="match_loss">';
				else
					echo '<div class="match_win">';

				if($returnArray[$x]['win'] == 0)
					echo '<div class="result">Loss</div>';
				else
					echo '<div class="result">Win</div>';

				echo '<div class = "champion"><div style="background-image: url(images/champions/'.$returnArray[$x]['champion'].'.png); background-size: contain; height: 72px; width: 72px;"></div></div>';

				echo '<div class="match_details"><small><i>'.$returnArray[$x]['tier_at_end'].' '.$returnArray[$x]['rank_at_end'].', LP: '.$returnArray[$x]['lp_at_end'].'</i></small><br><small>'.$returnArray[$x]['date'].'</small><br><small>'.floor((time() - strtotime($returnArray[$x]['date']))/3600).' Hours Ago</small></div>';

				echo '<div class="spells"><img src="images/summoner/'.$returnArray[$x]['spell1'].'.png"><img src="images/summoner/'.$returnArray[$x]['spell2'].'.png"></div>';

				echo '<div class="items">';

				if(isset($returnArray[$x]['item0']) && $returnArray[$x]['item0'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item0'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item1']) && $returnArray[$x]['item1'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item1'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item2']) && $returnArray[$x]['item2'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item2'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item3']) && $returnArray[$x]['item3'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item3'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item4']) && $returnArray[$x]['item4'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item4'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item5']) && $returnArray[$x]['item5'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item5'].'_32.png" alt="">';
				}
				   echo '<br></div>';
				
				echo '<div class="match_stats">'.$returnArray[$x]['kills'].' Kills<br>'.$returnArray[$x]['deaths'].' Deaths<br>'.$returnArray[$x]['assists'].' Assists</div>';

				echo '<div class="minions">'.$returnArray[$x]['minions'].'</div>';

				echo '<div class="gold">'.$returnArray[$x]['gold'].'g</div>';

				echo '<div class="booster">'.$returnArray[$x]['booster'].'</div>';

				echo '</div>';

				$x++;
				
				/*if($returnArray[$x]['win'] == 0)
					echo '<tbody class="match_loss" cellpadding="0" cellspacing="0">';
				else
					echo '<tbody class="match_win" cellpadding="0" cellspacing="0">';

				if($returnArray[$x]['win'] == 0)
					echo '<tr><td class="center">Loss</td>';
				else
					echo '<tr><td class="center">Win</td>';

				echo '<td><div style="background-image: url(images/champions/'.$returnArray[$x]['champion'].'.png); background-size: contain; height: 72px; width: 72px;"></div></td>';

				echo '<td class="center"><small><i>'.$tier.' '.$rank.', LP: '.$returnArray[$x]['lp_at_end'].'</i></small><br><small>'.$returnArray[$x]['date'].'</small><br><small>'.floor((time() - strtotime($returnArray[$x]['date']))/360).' Hours Ago</small></td>';

				echo '<td class="center summoner-spells"><img src="images/summoner/'.$returnArray[$x]['spell1'].'.png"><img src="images/summoner/'.$returnArray[$x]['spell2'].'.png"></td>';

				echo '<td class="center">';

				if(isset($returnArray[$x]['item0']) && $returnArray[$x]['item0'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item0'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item1']) && $returnArray[$x]['item1'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item1'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item2']) && $returnArray[$x]['item2'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item2'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item3']) && $returnArray[$x]['item3'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item3'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item4']) && $returnArray[$x]['item4'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item4'].'_32.png" alt="">';
				}
				if(isset($returnArray[$x]['item5']) && $returnArray[$x]['item5'] != 0)
				{
					echo '<img src="https://images.weserv.nl/?url=img.lolking.net/shared/riot/images/items/'.$returnArray[$x]['item5'].'_32.png" alt="">';
				}
				   echo '<br></td>';
				
				echo '<td class="center">'.$returnArray[$x]['kills'].' Kills<br>'.$returnArray[$x]['deaths'].' Deaths<br>'.$returnArray[$x]['assists'].' Assists</td>';

				echo '<td class="center">'.$returnArray[$x]['minions'].'</td>';

				echo '<td class="center">'.$returnArray[$x]['gold'].'g</td>';

				echo '<td class="center">'.$returnArray[$x]['booster'].'</td>';

				$x++;*/
}
//echo json_encode($returnArray);

?>